package com.g17.virtualpetbackend.service;

import com.g17.virtualpetbackend.entity.User;
import com.g17.virtualpetbackend.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> findAllUsers(){
        return userRepository.findAll();
    }

    public Optional<User> findUserById(Long id){
        return userRepository.findById(id);
    }

    public Optional<User> findUserByUsernameAndPassword(String username, String password){return userRepository.findByUsernameAndPassword(username, password); }

    public User createUser(String username, String password)
    {
        return userRepository.createUser(username, password);
    }

    public User updateUser(Long id, String username, String password, Integer type)
    {
        return userRepository.updateUser(id, username, password, type);
    }

    public User deleteUserById(Long id)
    {
        return userRepository.deleteUserById(id);
    }

    public List<User> findAllUserByUsernames(String[] usernames) {
        List<User> users = new ArrayList<>();
        for (String username:usernames) {
            Optional<User> userOptional= userRepository.findAllUsersByUsername(username);
            userOptional.ifPresent(users::add);
        }
        return users;
    }

}
